package bg.btanev.eightyfour.domain.profile.feed;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.Validate;

import bg.btanev.eightyfour.domain.Tag;
import bg.btanev.eightyfour.domain.geo.GeoLocation;
import bg.btanev.eightyfour.domain.profile.ProfileEvent;

/**
 *
 * @author B.Tanev
 *
 */
public class FeedProfileEvent extends ProfileEvent {
	/**
	 *
	 */
	private static final long serialVersionUID = 3084786475608739187L;

	private String guid; // unique; not null
	private String link;
	private String author;

	private List<FeedMediaContent> mediaContent = new ArrayList<FeedMediaContent>(); // component; idbag

	protected FeedProfileEvent() {
		super();
	}

	public FeedProfileEvent(Date date, FeedProfile profile, String guid) {
		this(null, date, profile, null, null, guid, null, null, null, null, null);
	}

	public FeedProfileEvent(FeedProfileEvent profileEvent, String guid, String link, String author) {

		this(profileEvent.getId(), profileEvent.getDate(), (FeedProfile) profileEvent.getProfile(), profileEvent
				.getTitle(), profileEvent.getDescription(), guid, link, author, profileEvent.getTags(), null,
				profileEvent.getGeoLocation());
	}

	public FeedProfileEvent(Date date, FeedProfile profile, String title, String description, String guid, String link,
			String author) {
		this(null, date, profile, title, description, guid, link, author, null, null, null);
	}

	public FeedProfileEvent(Date date, FeedProfile profile, String title, String description, String guid, String link,
			String author, Collection<Tag> tags, Collection<FeedMediaContent> mediaContent, GeoLocation geoLocation) {

		this(null, date, profile, title, description, guid, link, author, tags, mediaContent, geoLocation);
	}

	public FeedProfileEvent(Long id, Date date, FeedProfile profile, String title, String description, String guid,
			String link, String author, Collection<Tag> tags, Collection<FeedMediaContent> mediaContent,
			GeoLocation geoLocation) {

		super(id, date, profile, title, description, geoLocation);

		Validate.notEmpty(guid, "RSS profile event guid cannot be empty.");
		// there is a change for a link element to empty
		// Validate.notEmpty(link, "RSS profile event link cannot be empty.");
		if (link == null || link.length() == 0) {
			link = profile.getFeedUrl();
		}

		assert (link != null);

		this.guid = guid;
		this.link = link;
		this.author = author;

		if (tags != null) {
			this.addTags(tags);
		}
		if (mediaContent != null) {
			this.addMediaContent(mediaContent);
		}
	}

	public String getGuid() {
		return guid;
	}

	public String getLink() {
		return link;
	}

	public String getAuthor() {
		return author;
	}

	public boolean addMediaContent(FeedMediaContent mediaContent) {
		return this.mediaContent.add(mediaContent);
	}

	public boolean addMediaContent(Collection<FeedMediaContent> mediaContent) {
		return this.mediaContent.addAll(mediaContent);
	}

	public List<FeedMediaContent> getMediaContent() {
		return Collections.unmodifiableList(mediaContent);
	}

	@Override
	public String toString() {
		return "RssProfileEvent [author=" + author + ", guid=" + guid + ", link=" + link + ", toString()="
				+ super.toString() + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = prime + ((guid == null) ? 0 : guid.hashCode());
		result = prime * result + ((link == null) ? 0 : link.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (getClass() != obj.getClass())
			return false;
		FeedProfileEvent other = (FeedProfileEvent) obj;
		if (guid == null) {
			if (other.guid != null)
				return false;
		} else if (!guid.equals(other.guid))
			return false;
		if (link == null) {
			if (other.link != null)
				return false;
		} else if (!link.equals(other.link))
			return false;
		return true;
	}
}
